package com.xiaoxi.xirpc.server;

import io.vertx.core.Vertx;

public class VertxHttpServer implements HttpServer{
    @Override
    public void doStart(int port) {
        // 创建 Vert.x 实例
        Vertx vertx = Vertx.vertx();

        // 创建 http服务器
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();

        // 监听端口并处理请求
        server.requestHandler(new HttpServerHandler());

//        // 监听端口并处理请求
//        server.requestHandler(request -> {
//            // 处理http请求
//            System.out.println("received rquest:" + request.method() + " " + request.uri());
//
//            // 发送http响应
//            request.response().putHeader("content-type", "text/plain")
//                    .end("hello world from Vert.x http server!");
//        });

        // 启动http服务器并监听指定端口
        server.listen(port, result -> {
            if (result.succeeded()){
                System.out.println("server is now listening on port" + port);
            }else {
                System.out.println("failed to start server:" + result.cause());
            }
        });
    }
}
